package com.nbcb.service.impl;

import com.nbcb.dao.OrderDao;
import com.nbcb.dao.ProductDao;
import com.nbcb.domain.Order;
import com.nbcb.domain.Product;
import com.nbcb.service.OrderService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * 订单逻辑实现
 */
public class OrderServiceImpl implements OrderService {

    OrderDao orderDao;
    ProductDao productDao;

    @Override
    public Boolean createOrder(List<String> productId, String userId) {
        Order order = new Order();
        order.setId(UUID.randomUUID().toString());
        order.setProductListId(productId);
        order.setUserId(userId);

        Double d = 0.00;
        for(String id: productId){
            Product product = productDao.selectById(id);
            Double price = product.getPrice();
            d += price;
        }
        order.setTotalPrice(d);
        orderDao.save(order);


       /* Iterator<String> iterator = productId.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();

        }*/
        return true;
    }

    /**
     * 查询订单
     * @return
     */
    @Override
    public List<Order> queryOrder() {
        List<Order> orders = orderDao.selctAll();
        return orders;
    }
}
